package com.excemly.library.repository;

import com.excemly.library.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
// 添加继承
public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> {
    List<Book> findTop10ByAvailableCopiesGreaterThanOrderByIdDesc(Integer availableCopies);

    // 添加按借阅次数排序的查询方法
    @Query("SELECT b, COUNT(br.id) as borrowCount FROM Book b LEFT JOIN Borrow br ON b.id = br.book.id GROUP BY b.id ORDER BY borrowCount DESC")
    List<Object[]> findByBorrowCount(Pageable pageable);
}